/**
 * Copyright © 2009 Qasky. All rights reserved.
 *
 * 用户状态管理
 *
 * @author Wang Hongya
 * @version 0.0.0 2019/10/14
 *
 * History:
 * (author, date, desc)
 * (Wang Hongya, 20191014, 添加注释)
 */

import {loadSystemSrvName,confrimLogin,getUserPermissions,updPassword, findSysUsers,getUserRole,
    addSysUser, updSysUser, batchDelSysUsers,logout,existsSysUser,resetRetryCount,enableSysUsers,disableSysUsers,
    addUserRole,delUserRoles,findUserRoles,findUserResources,resetUserPasswd} from 'api/sysUserApi';

/* 用户的状态管理 */
const user = {
    state: {
        flag: false,
        sysUserDtos: [],
        page: {
            currentPage: 1,
            pageSize: 5,
            total: 0
        },
        resourceTree:[],
        queryPara: {},
        systemSrvName: '服务器密码机管理系统',
        fristAuthUKey: false,
        enalbedPswLogin: false,
        currentLoginType: "PSW",
    },
    mutations: {
        setSystemSrvName(state, obj) {
            if (obj.code === 0) {
                state.systemSrvName = obj.data.systemSrvName;
                state.fristAuthUKey = obj.data.fristAuthUKey;
                state.enalbedPswLogin = obj.data.enalbedPswLogin;
                state.currentLoginType = obj.data.currentLoginType;
            }
            sessionStorage.setItem('systemSrvName',state.systemSrvName);
            sessionStorage.setItem('fristAuthUKey',state.fristAuthUKey);
            sessionStorage.setItem('enalbedPswLogin',state.enalbedPswLogin);
            sessionStorage.setItem('currentLoginType',state.currentLoginType);
        },

        logout (state, vm) {
            sessionStorage.clear();
            // 恢复默认样式
            // let themeLink = document.querySelector('link[name="theme"]');
            // themeLink.setAttribute('href', '');
            // 清空打开的页面等数据，但是保存主题数据
            let theme = '';
            if (localStorage.theme) {
                theme = localStorage.theme;
            }
            //localStorage.clear();
            if (theme) {
                localStorage.theme = theme;
            }
        },
        findSysUsersMu(state, obj) {
            if (obj.rows !== '') {
                state.sysUserDtos = obj.rows;
                state.page.currentPage = obj.currentPage;
                state.page.pageSize = obj.pageSize;
                state.page.total = obj.total;
            }
        },
        saveResourceTreeMut(state, obj) {
          state.resourceTree = obj.data;
        },
        setQueryPara(state, obj){
          state.queryPara = obj;
        },
        setSoftVersion (state, obj) {
            if (obj.code === 0) {
                state.softVersion = obj.data;
            }

            sessionStorage.setItem('version',state.softVersion.version);
            sessionStorage.setItem('issueTime',state.softVersion.issueTime);
            sessionStorage.setItem('localQccsId',state.softVersion.localQccsId);
        },

    },
    actions: {
        /**
         * Function    : loadSoftVersionAct
         * Description : 加载应用系统版本信息
         * Input       : context注册Actions
         * Return      : none
         */
        /*loadSoftVersionAct(context) {
            return loadSoftVersion().then(response => {
                let data = response.data;
                context.commit('setSoftVersion', data);
            });
        },*/

      /**
       * Function    : loadSystemSrvNameAct
       * Description : 加载应用系统名称
       * Input       : context注册Actions
       * Return      : none
       */
      loadSystemSrvNameAct(context) {
        return loadSystemSrvName().then(response => {
          let data = response.data;
          context.commit('setSystemSrvName', data);
            return data;
        });
      },
      /**
       * Function    : comfrimLoginAct
       * Description : 确认用户登录
       * Input       : context注册Actions   para传递的请求参数
       * Return      : 返回数据
       */
      comfrimLoginAct(context, para) {
        return confrimLogin(para).then(response => {
          let data = response.data;
          return data;
        });
      },
      getUserPermissions(context, para) {
        return getUserPermissions(para).then(response => {
          return response.data;
        });
      },
      /*修改用户密码*/
      updPasswordAct(context, para) {
        return updPassword(para).then(response => {
          let data = response.data;
          return data;
        });
      },
      /*获取用户列表数据请求*/
      findSysUsersAct(context, para) {
        findSysUsers(para).then(response => {
          let data = response.data;
          context.commit('findSysUsersMu', data);
        });
      },
      /*添加用户*/
      addSysUserAct(context, para) {
        return addSysUser(para).then(response => {
          let data = response.data;
          return data;
        });
      },
      /*更新用户信息*/
      updSysUserAct(context, para) {
        return updSysUser(para).then(response => {
          let data = response.data;
          return data;
        });
      },
      /*批量删除用户*/
      batchDelSysUsersAct(context, para) {
        return batchDelSysUsers(para).then(response => {
          let data = response.data;
          return data;
        });
      },
      /*用户名查重*/
      existsSysUserAct(context, para) {
        return existsSysUser(para).then(response => {
          let data = response.data;
          return data;
        });
      },
      /*重置用户状态*/
      resetRetryCountAct(context, para) {
        return resetRetryCount(para).then(response => {
          let data = response.data;
          return data;
        });
      },
      /*退出登录*/
      logoutAct(context) {
        return logout().then(response => {
          let data = response.data;
          return data;
        });
      },
      /** 启用系统用户 **/
      enableSysUserAct(context, param) {
        return enableSysUsers(param).then(res => {
          return res.data;
        });
      },
      /** 禁用系统用户 **/
      disableSysUserAct(context, param) {
        return disableSysUsers(param).then(res => {
          return res.data;
        });
      },
      /** 重置密码 **/
      resetUserPasswdAct(context, param) {
        return resetUserPasswd(param).then(res => {
          return res.data;
        })
      },
      /** 新增用户角色 **/
      addUserRoleAct(context, param) {
        return addUserRole(param).then(res => {
          return res.data;
        });
      },
      /** 删除用户角色 **/
      delUserRoleAct(context, param) {
        return delUserRoles(param).then(res => {
          return res.data;
        });
      },
      /** 查询用户分配资源 **/
      findUserResourcesAct(context, param) {
        return findUserResources(param).then(res => {
          context.commit('saveResourceTreeMut', res.data);
          return res.data
        });
      },
      /** 查询用户分配的角色 **/
      findUserRolesAct(context, param) {
        return findUserRoles(param).then(data => {
          return data.data;
        });
      },

      /**获取用户角色*/
      getUserRoleAct(context, param) {
        return  getUserRole(param).then(data => {
          return data.data;
        });
      },
    }
};

export default user;
